package cn.tuling.org.service.impl;

import cn.tuling.basic.util.PageList;
import cn.tuling.org.domain.User;
import cn.tuling.org.mapper.UserMapper;
import cn.tuling.org.query.UserQuery;
import cn.tuling.org.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * @author 宋俊林
     * @date 2022/10/1 20:12
     * 查询所有用户信息
     */
    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

    /**
     * @author 宋俊林
     * @date 2022/10/1 22:07
     * 根据id查询
     */
    @Override
    public User findById(Long id) {
        return userMapper.findById(id);
    }

    /**
     * @author 宋俊林
     * @date 2022/10/2 17:45
     * 删除
     */
    @Override
    public void delete(Long id) {
        userMapper.delete(id);
    }

    /**
     * @author 宋俊林
     * @date 2022/10/1 22:31
     * 添加
     */
    @Override
    @Transactional
    public void add(User user) {
        user.setCreateTime(new Date());
        userMapper.add(user);
    }

    /**
     * @author 宋俊林
     * @date 2022/10/1 22:31
     * 修改
     */
    @Override
    @Transactional
    public void update(User user) {
        userMapper.update(user);
    }

    /**
     * @author 宋俊林
     * @date 2022/10/2 16:16
     * 分页查询
     */
    @Override
    public PageList<User> queryByPage(UserQuery userQuery) {
        Integer totals = userMapper.queryByCount(userQuery);
        if (null == totals || 0 == totals){
            return new PageList<>(0,new ArrayList<>());
        }
        List<User> users = userMapper.queryByPage(userQuery);
        return new PageList<>(totals,users);
    }
}
